class RankItem extends egret.DisplayObjectContainer {
	private headIcon: egret.Bitmap;
	private headMask: egret.Shape;

	private rankTxt: egret.TextField;
	private rankIcon: egret.Bitmap;
	private nameTxt: egret.TextField;
	private scoreContainer: egret.DisplayObjectContainer;
	private bottomLine: egret.Shape;
	public constructor(index: number, data: any, width:number=642,height: number = 108,isMine:boolean = false) {
		super();
		//let color = index % 2 == 0 ? 0x2f3036 : 0x37383e;
		let textColor = isMine? 0x0cb30c: 0x231f20;
		//背景
		// let bg = new egret.Shape();
		// bg.graphics.beginFill(color);
		// bg.graphics.drawRect(0, 0,width, height);
		// bg.graphics.endFill();
		// this.addChild(bg);

		if(!isMine){
			//底部的线
			// let lineColor = 0x3195ac;
			// let line = this.bottomLine = new egret.Shape();
			// line.graphics.beginFill(lineColor);
			// line.graphics.drawRect(15, height-2,(width-30), 2);
			// line.graphics.endFill();
			// this.addChild(line);
		}
		//排名
		if(data.rank>-1){
			let rank = this.rankTxt = new egret.TextField();
			rank.text = data.rank + "";
			rank.y = (height - rank.height) / 2;
			// rank.width = 100;
			rank.textAlign = "center";
			rank.x = 0;
			rank.width = 24*2+34;
			rank.textColor = textColor;
			//rank.italic = true;
			this.addChild(rank);
		}else{
			var rankIcon = this.rankIcon = new egret.Bitmap();
			rankIcon.x = 24;
			rankIcon.width = 34;
			rankIcon.height = 46;
			rankIcon.y = (height-rankIcon.height)/2;

			let rankIconimageLoader = new egret.ImageLoader();
			rankIconimageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
				let loader = <egret.ImageLoader>event.currentTarget;
				var bgtexture = new egret.Texture();
				bgtexture._setBitmapData(loader.data);
				this.rankIcon.texture = bgtexture;
			}, this);
			rankIconimageLoader.load("resource/assets/rank/rank"+data.rank+".png");
			this.addChild(rankIcon);
			
		}
		

		

		// if (index == 0) {
		// 	rank.textColor = 0xfa7e00;
		// } else if (index == 1) {
		// 	rank.textColor = 0xfdbf1e;
		// } else if (index == 2) {
		// 	rank.textColor = 0xfad313;
		// }

		
		//头像
		let headWH = 64;
		let imask = this.headMask = new egret.Shape();
		imask.graphics.beginFill(0x000000);
		imask.graphics.drawRoundRect(100, (height - headWH) / 2, headWH, headWH, 6, 6);
		imask.graphics.endFill();
		this.addChild(imask);

		var head = this.headIcon = new egret.Bitmap();
		head.width = head.height = headWH;
		head.mask = this.headMask;

		let imageLoader = new egret.ImageLoader();
		imageLoader.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
			let imageLoader = <egret.ImageLoader>event.currentTarget;
			var bgtexture = new egret.Texture();
			bgtexture._setBitmapData(imageLoader.data);
			this.headIcon.texture = bgtexture;
		}, this);
		imageLoader.load(data.avatarUrl);
		this.addChild(head);

		head.x = 100;
		head.y = (height - headWH) / 2;;

		
		//名字
		let nicktxt = this.nameTxt = new egret.TextField();
		nicktxt.x = 200;
		nicktxt.text = ParseUtils.getInstance().parseName(data.nickname);
		nicktxt.y = (height - nicktxt.height) / 2;
		nicktxt.size = 30;
		nicktxt.textColor = textColor;
		this.addChild(nicktxt);

		//let score = data.weekScore;
		let score = data.highestScore
		if (GlobalData.textureArr && GlobalData.textureArr.length == 10) {
			// this.scoreContainer = new egret.DisplayObjectContainer();
			// this.addChild(this.scoreContainer);

			// for(let i=0;i<score.length;i++){
			// 	let sn=parseInt(score[i]);
			// 	let bitmap=new egret.Bitmap(GlobalData.textureArr[sn]);
			// 	bitmap.x=this.scoreContainer.width;
			// 	this.scoreContainer.addChild(bitmap);
			// }
			
			// let scale=25/this.scoreContainer.height;
			// this.scoreContainer.scaleX=this.scoreContainer.scaleY=scale;
			// this.scoreContainer.y = (height - 25) / 2;
			// this.scoreContainer.x=width-this.scoreContainer.width*this.scoreContainer.scaleX-37;

			//分数
			let numtxt = new egret.TextField();
			numtxt.x = 440;
			numtxt.width = 200;
			numtxt.textAlign = egret.HorizontalAlign.LEFT;
			// numtxt.fontFamily=GlobalData.fontFamliy;
			//numtxt.size = 50;
			//numtxt.bold = true;
			numtxt.text = score;
			numtxt.textColor = textColor;
			numtxt.y = (height - numtxt.height) / 2;
			this.addChild(numtxt);
		} else {
			//分数
			let numtxt = new egret.TextField();
			numtxt.x = 440;
			numtxt.width = 200;
			numtxt.textAlign = egret.HorizontalAlign.LEFT;
			// numtxt.fontFamily=GlobalData.fontFamliy;
			//numtxt.size = 50;
			//numtxt.bold = true;
			numtxt.textColor = textColor;
			numtxt.text = score;
			numtxt.y = (height - numtxt.height) / 2;
			this.addChild(numtxt);
		}

	}

	public clearItem()
	{
		if(this.headIcon && this.headIcon.parent)
		{
			this.headIcon.parent.removeChild(this.headIcon);
		}
		if(this.headMask && this.headMask.parent)
		{
			this.headMask.parent.removeChild(this.headMask);
		}
		if(this.rankTxt && this.rankTxt.parent)
		{
			this.rankTxt.parent.removeChild(this.rankTxt);
		}
		if(this.rankIcon && this.rankIcon.parent)
		{
			this.rankIcon.parent.removeChild(this.rankIcon);
		}
		if(this.nameTxt && this.nameTxt.parent)
		{
			this.nameTxt.parent.removeChild(this.nameTxt);
		}
		if(this.bottomLine && this.bottomLine.parent)
		{
			this.bottomLine.parent.removeChild(this.bottomLine);
		}
		if(this.scoreContainer && this.scoreContainer.parent)
		{
			this.scoreContainer.parent.removeChild(this.scoreContainer);
		}
	}
}